﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cms.BLL;
using System.Collections;
using Cms.EntLib;

namespace Cms.Web.CmsSystem.SysAdmin.FormSetting
{
    /// <summary>
    /// DrawForm1 的摘要说明
    /// </summary>
    public class DrawForm1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string act = EntLib.Request.GetString("act");
            string formid = Request.GetString("formid");
            string result = "{}";
            switch (act.ToLower())
            {
                case "getallformlist":
                    result = GetFormList();
                    break;
                case "getformjson":
                    result = GetFromJson(formid);
                    break;
                case "savedesignform":
                    string jsondata = EntLib.Request.GetString("jsondata");
                    result = SaveDesignFormLayOut(formid, jsondata);
                    break;
                default:
                    break;
            }
            ResponseText(result);
        }
        /// <summary>
        /// 获取表单列表
        /// </summary>
        public string GetFormList()
        {
            BLL.Sys_Selfform bll = new Sys_Selfform();
            IList result = bll.Find("select t.Id,t.Caption from Sys_Selfform t", null);
            string textjson = Json.ToJson(result);
            return textjson;
        }
        public string GetFromJson(string formid)
        {
            string result = "";
            BLL.Sys_Selfform bll = new Sys_Selfform();
            Model.Sys_Selfform model = bll.GetById(formid, false);
            if (model != null)
            {
                result = Json.ToJson(model);
            }
            return result;
        }
        public string SaveDesignFormLayOut(string formid, string json)
        {
            string result = "{Result:false}";
            BLL.Sys_Selfform bllfrom = new Sys_Selfform();
            Model.Sys_Selfform model = bllfrom.GetById(formid, false);
            if (model != null)
            {
                if (model.Layout.Count > 0)
                {
                    foreach (Model.Sys_Selfform_Layout layout in model.Layout)
                    {
                        layout.Jsondata = json;
                        layout.Updatedate = DateTime.Now;
                        break;
                    }
                }
                else
                {
                    Model.Sys_Selfform_Layout modelLayout = new Model.Sys_Selfform_Layout();
                    modelLayout.SelfForm = model;
                    modelLayout.Id = System.Guid.NewGuid().ToString();
                    modelLayout.Jsondata = json;
                    modelLayout.Updatedate = DateTime.Now;
                    modelLayout.Width = 800;
                    modelLayout.Height = 600;
                    modelLayout.Enabled = true;
                    model.Layout.Add(modelLayout);
                }
            }
            bllfrom.SaveOrUpdate(model);
            bllfrom.CommitChanges();
            result = "{ Result:true}";
            return result;
        }
        void ResponseText(string text)
        {
            //HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            HttpContext.Current.Response.Expires = -1;
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(text);
            HttpContext.Current.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}